# -*- coding:utf-8 -*-

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from AutoTestBDC.WorkFlow.User import User
from AutoTestBDC.extScript.GetList import GetListValue
from AutoTestBDC.extScript.SeaValue import SeaValue
from time import sleep
import os
from AutoTestBDC.extScript.UploadPic import UploadPic

class RWJG(User):
    def __init__(self,driver,username,userpwd,type=None,caseno=None,qlrtype=None):
        super().Login(driver,username, userpwd)
        self.driver = driver
        self.type = type
        self.caseno = caseno
        self.qlrtype = qlrtype

        self.test_tester()

    def test_tester(self):
        glv = GetListValue()
        if self.qlrtype == "自然人":
            tablexpath = "/html/body/content/div/form/div/div[19]"

        if self.qlrtype == "法人机构":
            tablexpath = "/html/body/content/div/form/div/div[20]"

        if self.qlrtype == "其他组织":
            tablexpath = "/html/body/content/div/form/div/div[21]"

        if self.type == "接受":
            self.driver.find_element_by_link_text(u"待办理").click()
            SeaValue(self.driver, self.caseno, self.qlrtype)
            glv.clickvalue(self.driver, tablexpath, self.caseno)
            self.set_acceptance()

        if self.type == "不接受":
            self.driver.find_element_by_link_text(u"待办理").click()
            SeaValue(self.driver, self.caseno, self.qlrtype)
            glv.clickvalue(self.driver, tablexpath, self.caseno)
            self.set_refused()

        if self.type == "成果提交":
            self.driver.find_element_by_link_text(u"办理中").click()
            SeaValue(self.driver, self.caseno, self.qlrtype)
            glv.clickvalue(self.driver, tablexpath, self.caseno)
            self.set_commitres()

        super().Logout()

    def set_acceptance(self):
        self.driver.execute_script("arguments[0].scrollIntoView();",
                                   self.driver.find_element_by_xpath("/html/body/content/div/form/div[1]/div[1]"))

        self.driver.find_element_by_xpath("/html/body/content/div/form/div[1]/div[3]/input[1]").click()
        self.driver.find_element_by_xpath("/html/body/content/div/div[49]/div[3]/button[7]").click()

    def set_refused(self):
        self.driver.execute_script("arguments[0].scrollIntoView();",
                                   self.driver.find_element_by_xpath("/html/body/content/div/form/div[1]/div[1]"))
        self.driver.find_element_by_xpath("(//input[@name='shortlistedAgencyBusiness.accepted'])[2]").click()#"/html/body/content/div/form/div[1]/div[3]/input[2]"
        self.driver.find_element_by_id("received_text").clear()
        self.driver.find_element_by_id("received_text").send_keys(u"任性，就是不想接受而已。")
        self.driver.find_element_by_id("detail-resubmit").click()

    def set_commitres(self):
        self.driver.execute_script("arguments[0].scrollIntoView();",
                                   self.driver.find_element_by_xpath("/html/body/content/div/form/div[4]/div[1]"))

        #self.driver.find_element_by_xpath('/html/body/content/div/form/div[4]/div[3]/div[1]/div[1]/div[2]/div/input').click()

        UploadPic(self.driver, "/html/body/content/div/form/div[4]/div[3]/div[1]/div[1]/div[2]/div"
                  ,"inputdata\\工作成果.zip").upfile(
            "/html/body/content/div/form/div[4]/div[3]/div[1]/div[1]/div[2]/a")

        sleep(5)

        # count = 0
        # while 1 == 1:
        #     if count == 100:
        #         break
        #     else:
        #         sleep(1)
        #     #count = int((self.driver.find_element_by_xpath("/html/body/content/div/form/div[4]/div[3]/div[1]/div[2]/div/div")).get_attribute("aria-valuenow"))
        #     count = int((self.driver.find_element_by_class_name("progress-bar\ progress-bar-success\ progress-bar-striped\ text-center")).get_attribute("aria-valuenow"))

        self.driver.find_element_by_id("detail-tijiao").click()
        sleep(2)
        self.driver.find_element_by_css_selector("div.modal_container.modal_receive > button.modal_confirm").click()
